package com.kykj.kykj.controller;

import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.entity.NoticeEntity;
import com.kykj.kykj.service.NoticeService;
import com.kykj.kykj.service.impl.NoticeServiceImpl;
import com.kykj.kykj.vo.NoticeVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/notices/")
@CrossOrigin//解决跨域问题
@RestController
public class NoticeController {
    @Resource
    NoticeServiceImpl noticeService;

    /**
     * 获取所有公告记录
     * @return resultEntity
     */
    @GetMapping("getnoticelist")
    private ResultEntity getNoticeList() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(noticeService.getNoticeList());
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    /**
     * 添加公告记录
     * @param noticeVo 公告记录 VO
     * @return resultEntity
     */
    @PostMapping("notices")
    private ResultEntity addNotice(@RequestBody NoticeVo noticeVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (noticeService.addNotice(noticeVo) > 0) {
                System.out.println("当前自增的id是" + noticeVo.getId() + "------自增");
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功！");
            } else {
                resultEntity.setCode(1);
                resultEntity.setMsg("添加失败！" + noticeVo.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    /**
     * 删除公告记录
     * @param id 公告记录 ID
     * @return resultEntity
     */
    @DeleteMapping("notices/{id}")
    private ResultEntity deleteNotice(@PathVariable("id") int id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (noticeService.delNotice(id) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode(1);
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    /**
     * 更新公告记录
     * @param noticeVo 公告记录 VO
     * @return resultEntity
     */
    @PutMapping("notices")
    private ResultEntity updateNotice(@RequestBody NoticeVo noticeVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (noticeService.upNotice(noticeVo) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功！");
            } else {
                resultEntity.setCode(1);
                resultEntity.setMsg("修改失败！");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    /**
     * 根据 ID 获取公告记录
     * @param id 公告 ID
     * @return resultEntity
     */
    @GetMapping("getnoticebyid")
    private ResultEntity getNoticeById(@RequestParam("id") int id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(noticeService.getNoticeById(id));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    /**
     * 获取公告总数
     * @return  resultEntity
     */
    @GetMapping("getnoticecount")
    private ResultEntity getNoticeCount() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(noticeService.getNoticeCount());
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }
}